//
//  UserModel.m
//  DingDing
//
//  Created by LHL on 14/11/24.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "UserModel.h"
#import "AFNetworking.h"
@implementation UserModel

@end

@implementation photoModel
-(id)init{
    if (self = [super init]) {
        self.photos = [[NSMutableArray alloc]init];
        self.details = [[NSMutableArray alloc]init];
    }
    return self;
}

@end

@implementation phoneLogin
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    [self gotoPage];
}

- (void)nextPage
{
    
}

- (void)gotoPage
{
    [API_DINGDING_GET_PHONELOGIN cancel];
    
    API_DINGDING_GET_PHONELOGIN * api = [API_DINGDING_GET_PHONELOGIN api];
    
    @weakify(api);
    @weakify(self);
    api.req.phone = self.phone;
    api.req.password = self.password;
    api.whenUpdate = ^
    
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.message = api.resp.msg;
                    [Common saveUserid:api.resp.data];
                    
                    if (api.resp.data.pet_info.count >0) {
                        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"isaddpet"];
                        [[NSUserDefaults standardUserDefaults]synchronize];
                    }else{
                        [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"isaddpet"];
                        [[NSUserDefaults standardUserDefaults]synchronize];
                    }
                    if (![self.code isEqualToString:@"200"]) {
                        [self sendUISignal:self.RELOADED];
                        return ;
                    }
//                    AFHTTPRequestOperation * op = [[AFHTTPRequestOperation alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.dddd.gg/index.php/pet/updateUserPlace?user_id=%@&lat=%@&lng=%@",api.resp.data.user_id,[[NSUserDefaults standardUserDefaults] objectForKey:@"lat"],[[NSUserDefaults standardUserDefaults] objectForKey:@"lnt"]]]]];
//                    
//                    [op start];
//                    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
//                            NSDictionary *tt = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
//                        NSLog(@"update location success ---%@",tt);
//                    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
//                        NSLog(@"update location error is %@",error);
//                    }];
//                    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:api.resp.data.im_username
//                                                                        password:api.resp.data.im_password
//                                                                      completion:
//                     ^(NSDictionary *loginInfo, EMError *error) {
//
//                         if (loginInfo && !error) {
//                             [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
//                             EMPushNotificationOptions *option = [[EMPushNotificationOptions alloc]init];
//                             option.displayStyle = ePushNotificationDisplayStyle_simpleBanner;
//                             if (api.resp.data.pet_info.count > 0) {
//                                 PETINFOMODEL *model = api.resp.data.pet_info[0];
//                                 [[EaseMob sharedInstance].chatManager setNickname:model.petname];
//                             }
//                             [[EaseMob sharedInstance].chatManager asyncUpdatePushOptions:option];
//                         }else {
//                             switch (error.errorCode) {
//                                 case EMErrorServerNotReachable:
//                                     TTAlertNoTitle(@"连接服务器失败!");
//                                     break;
//                                 case EMErrorServerAuthenticationFailure:
//                                     TTAlertNoTitle(@"用户名或密码错误");
//                                     break;
//                                 case EMErrorServerTimeout:
//                                     TTAlertNoTitle(@"连接服务器超时!");
//                                     break;
//                                 default:
//                                     TTAlertNoTitle(@"登录失败");
//                                     break;
//                             }
//                         }
//                     } onQueue:nil];
                    [self sendUISignal:self.RELOADED withObject:@"登录成功"];
                    
                    self.loaded = YES;
                    
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}


@end



@implementation phoneRegister
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    [self gotoPage];
}

- (void)nextPage
{
    
}

- (void)gotoPage
{
    [API_DINGDING_GET_PHONEREGISTER cancel];
    
    API_DINGDING_GET_PHONEREGISTER * api = [API_DINGDING_GET_PHONEREGISTER api];
    
    @weakify(api);
    @weakify(self);
    api.req.phone = self.phone;
    api.req.password = self.password;
    api.req.verifyCode = self.verifyCode;
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.message = api.resp.msg;
                    [Common saveUserid:api.resp.data];
                    [Common saveUserInfo:api.req.phone pwd:api.req.password];
                    
                    [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"logout"];
                    [[NSUserDefaults standardUserDefaults]setObject:@"phone" forKey:@"logintype"];
                    [self sendUISignal:self.RELOADED];
                    self.loaded = YES;
                    return ;
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}


@end

@implementation sendMessageCode
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    [self gotoPage];
}

- (void)nextPage
{
    
}

- (void)gotoPage
{
    [API_DINGDING_GET_PHONEVERIFYCODE cancel];
    
    API_DINGDING_GET_PHONEVERIFYCODE * api = [API_DINGDING_GET_PHONEVERIFYCODE api];
    
    @weakify(api);
    @weakify(self);
    api.req.phone = self.phone;
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.message = api.resp.msg;
                    
                    [self sendUISignal:self.RELOADED];
                    self.loaded = YES;
                    return ;
                    
                }
            }else{
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
        }
    };
    [api send];
}


@end

@implementation addPetHead
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    NSMutableDictionary *dic =[[NSMutableDictionary alloc]init];
    [dic setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"] forKey:@"user_id"];
    if (!(self.pet_id == nil || [self.pet_id isEqualToString:@""])) {
        [dic setObject:self.pet_id forKey:@"pet_id"];
    }
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:@"http://www.dddd.gg/index.php/pet/add_pet_head" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        [formData appendPartWithFileData:[[NSUserDefaults standardUserDefaults]objectForKey:@"head_img"] name:@"file1" fileName:@"images" mimeType:@"image/jpeg"];
    }error:nil];
    AFHTTPRequestOperation *af = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    [af start];
    [af setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
        NSLog(@"success");
        [self sendUISignal:self.RELOADED];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        [self sendUISignal:self.FAILED];
        NSLog(@"%@",error);
    }];
    [self sendUISignal:self.RELOADING];
//    [self gotoPage];
}
- (void)nextPage
{
    NSMutableDictionary *dic =[[NSMutableDictionary alloc]init];
    [dic setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"] forKey:@"user_id"];
    if (!(self.pet_id == nil || [self.pet_id isEqualToString:@""])) {
        [dic setObject:self.pet_id forKey:@"pet_id"];
    }
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:@"http://www.dddd.gg/index.php/pet/update_pet_head" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        [formData appendPartWithFileData:[[NSUserDefaults standardUserDefaults]objectForKey:@"head_img"] name:@"file1" fileName:@"images" mimeType:@"image/jpeg"];
    }error:nil];
    AFHTTPRequestOperation *af = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    [af start];
    [af setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
        NSLog(@"success");
        [self sendUISignal:self.RELOADED];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        [self sendUISignal:self.FAILED];
        NSLog(@"%@",error);
    }];
    [self sendUISignal:self.RELOADING];
}

- (void)gotoPage
{
    [API_DINGDING_POST_ADD_PET_HEAD cancel];
    
    API_DINGDING_POST_ADD_PET_HEAD * api = [API_DINGDING_POST_ADD_PET_HEAD api];
    
    @weakify(api);
    @weakify(self);
    api.req.user_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"];
    api.req.file1 = self.file1;
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    
                    self.code = api.resp.code;
                    self.message = api.resp.msg;
                    [[NSUserDefaults standardUserDefaults]setObject:self.file1 forKey:@"head_img"];
                    [[NSUserDefaults standardUserDefaults]synchronize];
                    self.loaded = YES;
                    return ;
                    
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}


@end


@implementation addPets
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    [self gotoPage];
}

- (void)nextPage
{
    
}

- (void)gotoPage
{
    [API_DINGDING_GET_ADDPET cancel];
    
    API_DINGDING_GET_ADDPET * api = [API_DINGDING_GET_ADDPET api];
    
    @weakify(api);
    @weakify(self);
    api.req.user_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"];
    api.req.pet_id = self.pet_id;
//    api.req.token = self.token; //暂时未用
    api.req.petname = self.petname;
    api.req.gender = self.gender;
    api.req.age = self.age;
    api.req.variety = self.variety;
    api.req.lat = [[NSUserDefaults standardUserDefaults]objectForKey:@"lat"];
    api.req.lng = [[NSUserDefaults standardUserDefaults]objectForKey:@"lnt"];
    
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.message = api.resp.msg;
                    
                    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
                    if ([self.code isEqualToString:@"200"]) {
                        [users setObject:@"1" forKey:@"isaddpet"];
                        [users setObject:self.pet_id forKey:@"pet_id"];
                        [users setObject:self.petname forKey:@"petname"];
                        [users setObject:self.address forKey:@"address"];
                        [users setObject:self.gender forKey:@"gender"];
                        [users setObject:self.age forKey:@"age"];
                        [users synchronize];
                    }
                    EMPushNotificationOptions *option = [[EMPushNotificationOptions alloc]init];
                    option.displayStyle = ePushNotificationDisplayStyle_simpleBanner;
                    [[EaseMob sharedInstance].chatManager setNickname:self.petname];
                    [[EaseMob sharedInstance].chatManager asyncUpdatePushOptions:option];
                    [self sendUISignal:self.RELOADED];
                    self.loaded = YES;
                    return ;
                    
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}


@end


@implementation getPetList
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    [self gotoPage];
}

- (void)nextPage
{
    
}
- (void)gotoPage
{
    [API_DINGDING_GET_PETLIST cancel];
    
    API_DINGDING_GET_PETLIST * api = [API_DINGDING_GET_PETLIST api];
    
    @weakify(api);
    @weakify(self);
    api.req.phone=@"";
    
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.message = api.resp.msg;
                    self.petlist = api.resp.data;
                    [self sendUISignal:self.RELOADED];
                    self.loaded = YES;
                    return ;
                    
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}


@end

@implementation getNearbyUser
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    self.data = [[NSMutableArray alloc]init];
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    [self.data removeAllObjects];
    [self gotoPage:1];
}

-(void)nextPage{
    if (self.more == NO) {
        return;
    }
    [self gotoPage:(self.data.count/20)+1];
}
- (void)gotoPage:(NSInteger)req
{
    [API_DINGDING_GET_NEARBYUSER cancel];
    
    API_DINGDING_GET_NEARBYUSER * api = [API_DINGDING_GET_NEARBYUSER api];
    
    @weakify(api);
    @weakify(self);
    api.req.user_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"];
    api.req.token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    api.req.lat = [[NSUserDefaults standardUserDefaults]objectForKey:@"lat"];
    api.req.lng = [[NSUserDefaults standardUserDefaults]objectForKey:@"lnt"];
    api.req.lat = @"30.5488790000";
    api.req.lng = @"104.0772720000";
    api.req.pagesize = @"20";
    api.req.page = [NSString stringWithFormat:@"%ld",(long)req];
    
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.message = api.resp.msg;
                    [self.data addObjectsFromArray:api.resp.data];
                    for (NEARBYMODEL *model in self.data) {
                        if ([model.user_id isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]]) {
                            [self.data removeObject:model];
                            break;
                        }
                    }
                    self.more = YES;
                    [self sendUISignal:self.RELOADED];
                    self.loaded = YES;
                    return ;
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}

@end

@implementation getUserInfo{
    NSInteger count;
}
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
//    self.data = [[NSMutableArray alloc]init];
    count = 0;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
//    [self.data removeAllObjects];
    [self gotoPage:1];
}

-(void)nextPage{
    if (self.more == NO) {
        return;
    }
    [self gotoPage:(count/20)+1];
}
- (void)gotoPage:(NSInteger)req
{
    [API_DINGDING_GETUSERINFO cancel];
    
    API_DINGDING_GETUSERINFO * api = [API_DINGDING_GETUSERINFO api];
    
    @weakify(api);
    @weakify(self);
    api.req.user_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"];
    api.req.token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    api.req.fid = self.fid;
    api.req.pagesize = @"20";
    api.req.page = [NSString stringWithFormat:@"%ld",(long)req];
    
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.message = api.resp.msg;
                    self.data = api.resp.data;
                    self.more = api.resp.data.item.count-1<=20 ? NO : YES;
                    count += api.resp.data.item.count;
                    [self sendUISignal:self.RELOADED];
                    self.loaded = YES;
                    return ;
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}

@end


@implementation oneKeyLogin{
    NSInteger count;
}
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    //    self.data = [[NSMutableArray alloc]init];
    count = 0;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    //    [self.data removeAllObjects];
    [self gotoPage];
}

-(void)nextPage{
//    if (self.more == NO) {
//        return;
//    }
    [self gotoPage];
}
- (void)gotoPage
{
    [API_DINGDING_LOGIN cancel];
    
    API_DINGDING_LOGIN * api = [API_DINGDING_LOGIN api];
    
    @weakify(api);
    @weakify(self);
    api.req.vendor = self.vendor;
    api.req.access_token = self.access_token;
    api.req.openid = self.openid;
    api.req.nickname = self.nickname;
    api.req.timestamp = self.timestamp;
    api.req.expires_in = self.expires_in;
    api.req.device_type = @"iOS";
    api.req.device_id = [NSString stringWithFormat:@"%s",[BeeSystemInfo deviceUUID].UTF8String];
    
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.msg = api.resp.msg;
                    self.thirdData = api.resp.data;
                    [Common saveThirdUserInfo:api.resp.data];

                    [self sendUISignal:self.RELOADED];
                    self.loaded = YES;
                    return ;
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}

@end


@implementation getPetInfoByID{
    NSInteger count;
}
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    //    self.data = [[NSMutableArray alloc]init];
    count = 0;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    //    [self.data removeAllObjects];
    [self gotoPage];
}

-(void)nextPage{
    //    if (self.more == NO) {
    //        return;
    //    }
    [self gotoPage];
}
- (void)gotoPage
{
    [API_DINGDING_GETPETNAMEBYID cancel];
    
    API_DINGDING_GETPETNAMEBYID * api = [API_DINGDING_GETPETNAMEBYID api];
    
    @weakify(api);
    @weakify(self);
    api.req.p_id = self.p_id;
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.msg = api.resp.msg;
                    self.name = api.resp.data.name;
                    [self sendUISignal:self.RELOADED];
                    self.loaded = YES;
                    return ;
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}

@end

@implementation updatePetInfo{
    NSInteger count;
}
- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    //    self.data = [[NSMutableArray alloc]init];
    count = 0;
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

#pragma mark -

- (void)firstPage
{
    //    [self.data removeAllObjects];
    [self gotoPage];
}

-(void)nextPage{
    //    if (self.more == NO) {
    //        return;
    //    }
    [self gotoPage];
}
- (void)gotoPage
{
    [API_DINGDING_GET_CHANGEPETINFOER cancel];
    
    API_DINGDING_GET_CHANGEPETINFOER * api = [API_DINGDING_GET_CHANGEPETINFOER api];
    
    @weakify(api);
    @weakify(self);
    api.req.user_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"];
    api.req.token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    api.req.lat = [[NSUserDefaults standardUserDefaults]objectForKey:@"lat"];
    api.req.lng = [[NSUserDefaults standardUserDefaults]objectForKey:@"lnt"];
    api.req.petname = self.petname;
    api.req.signature = self.signature;
    api.req.gender = self.gender;
    api.req.age = self.age;
    api.req.variety = self.variety;
    api.req.pet_id = self.pet_id;
    api.req.address = self.address;
    api.whenUpdate = ^
    {
        @normalize(api);
        @normalize(self);
        
        if ( api.sending )
        {
            
            [self sendUISignal:self.RELOADING];
        }
        else
        {
            
            if ( api.succeed )
            {
                if ( nil == api.resp )
                {
                    api.failed = YES;
                }
                else
                {
                    self.code = api.resp.code;
                    self.msg = api.resp.msg;
                    if ([self.code isEqualToString:@"200"]) {
                        NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
                        [users setObject:api.req.lat forKey:@"lat"];
                        [users setObject:api.req.lng forKey:@"lnt"];
                        [users setObject:api.req.petname forKey:@"petname"];
                        [users setObject:api.req.signature forKey:@"signature"];
                        [users setObject:api.req.gender forKey:@"gender"];
                        [users setObject:api.req.age forKey:@"age"];
                        [users setObject:api.req.variety forKey:@"variety"];
                        [users setObject:api.req.address forKey:@"address"];
                        [users synchronize];
                        
                    }
                    [self sendUISignal:self.RELOADED];
                    self.loaded = YES;
                    return ;
                }
            }else{
                
                [self sendUISignal:self.RELOADED withObject:api.resp.msg];
            }
            
            
        }
    };
    [api send];
}

@end


@implementation postdynamics

- (void)load
{
    self.autoSave = YES;
    self.autoLoad = YES;
    self.imgData = [[NSMutableArray alloc]init];
    
}

- (void)unload
{
    
}

#pragma mark -

- (void)loadCache
{
    
}

- (void)saveCache
{
}

- (void)clearCache
{
    //	[self.shots removeAllObjects];
    
    //	[SHOT removeObjectForKey:self.type];
}

- (void)firstPage
{
    NSMutableDictionary *dic =[[NSMutableDictionary alloc]init];
    [dic setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"] forKey:@"user_id"];
    [dic setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"token"] forKey:@"token"];
    [dic setObject:self.text forKey:@"text"];
    //post_id 暂时未启用;
    NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"lat"];
    NSString *lnt = [[NSUserDefaults standardUserDefaults] objectForKey:@"lnt"];
    if (lat == nil || lnt == nil) {
        [dic setObject:@"30.5488790000" forKey:@"lat"];
        [dic setObject:@"104.0772720000" forKey:@"lng"];
    }else{
        [dic setObject:lat forKey:@"lat"];
        [dic setObject:lnt forKey:@"lng"];
    }

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager POST:@"http://www.dddd.gg/index.php/pet/post" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        for (int i = 0; i <self.imgData.count; i++) {
            [formData appendPartWithFileData:UIImageJPEGRepresentation(self.imgData[i], 0.75) name:[NSString stringWithFormat:@"file%d",i+1] fileName:[NSString stringWithFormat:@"file%d.jpg",i+1] mimeType:@"image/jpeg"];
        }
    }success:^(AFHTTPRequestOperation *operation, id responseObject){
        NSLog(@"success");
        NSDictionary *tt = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        self.code = [tt objectForKey:@"code"];
        self.msg = [tt objectForKey:@"msg"];
        NSLog(@"ttt--%@\n---:%@",tt,self.msg);
        [self sendUISignal:self.RELOADED];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        [self sendUISignal:self.FAILED];
        NSDictionary *tt = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        self.code = [tt objectForKey:@"code"];
        self.msg = [tt objectForKey:@"msg"];
//        NSLog(@"%ld----%@-----%@",(long)[error code],[error localizedFailureReason],[error localizedDescription]);
    }];
     [self sendUISignal:self.RELOADING];

}

@end

@implementation checkUserExist
- (void)firstPage{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:@"http://www.dddd.gg/index.php/pet/checkUserExist" parameters:@{@"phone":self.phone} success:^(AFHTTPRequestOperation *operation, id responseObject){
        
//        NSLog(@"%@",responseObject);
        self.code = [responseObject objectForKey:@"code"];
        self.msg = [responseObject objectForKey:@"msg"];
        [self sendUISignal:self.RELOADED];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSDictionary *tt = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        self.code = [tt objectForKey:@"code"];
        self.msg = [tt objectForKey:@"msg"];
        [self sendUISignal:self.FAILED];
    }];
    [self sendUISignal:self.RELOADING];
}

@end

@implementation codeVerify
-(void)firstPage{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:@"http://www.dddd.gg/index.php/pet/checkVerifyCode" parameters:@{@"phone":self.phone,@"verifycode":self.verifycode} success:^(AFHTTPRequestOperation *operation, id responseObject){
//        NSLog(@"%@",responseObject);
        self.code = [responseObject objectForKey:@"code"];
        self.msg = [responseObject objectForKey:@"msg"];
        [self sendUISignal:self.RELOADED];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSDictionary *tt = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        self.code = [tt objectForKey:@"code"];
        self.msg = [tt objectForKey:@"msg"];
        [self sendUISignal:self.FAILED];
    }];
    [self sendUISignal:self.RELOADING];
}

@end

@implementation forgotPassword
-(void)firstPage{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:@"http://www.dddd.gg/index.php/pet/forgotPassword" parameters:@{@"phone":self.phone,@"newpassword":self.newpassword} success:^(AFHTTPRequestOperation *operation, id responseObject){
//        NSLog(@"%@",responseObject);
        self.code = [responseObject objectForKey:@"code"];
        self.msg = [responseObject objectForKey:@"msg"];
        [self sendUISignal:self.RELOADED];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSDictionary *tt = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        self.code = [tt objectForKey:@"code"];
        self.msg = [tt objectForKey:@"msg"];
        [self sendUISignal:self.FAILED];
    }];
}

@end

@implementation changePassword
-(void)firstPage{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"]);
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:@"http://www.dddd.gg/index.php/pet/changePassword" parameters:@{@"user_id":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"],@"newpassword":self.newpassword,@"oldpassword":self.oldpassword} success:^(AFHTTPRequestOperation *operation, id responseObject){
//        NSLog(@"%@",responseObject);
        self.code = [responseObject objectForKey:@"code"];
        self.msg = [responseObject objectForKey:@"msg"];
        [self sendUISignal:self.RELOADED];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSDictionary *tt = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        self.code = [tt objectForKey:@"code"];
        self.msg = [tt objectForKey:@"msg"];
        [self sendUISignal:self.FAILED];
    }];
}

@end

@implementation updatePetHead
-(void)firstPage{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    NSString *user = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"] forKey:@"user_id"];
    [dic setObject:self.pet_id forKey:@"pet_id"];
    [manager POST:@"http://www.dddd.gg/index.php/pet/update_pet_head" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
            [formData appendPartWithFileData:[[NSUserDefaults standardUserDefaults]objectForKey:@"head_img"] name:@"file1" fileName:@"file1" mimeType:@"image/jpeg"];
        
    }success:^(AFHTTPRequestOperation *operation, id responseObject){
        NSLog(@"%@",responseObject);
        [self sendUISignal:self.RELOADED];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
    NSDictionary *tt = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        self.code = [tt objectForKey:@"code"];
        self.msg = [tt objectForKey:@"msg"];
        [self sendUISignal:self.FAILED];
//        NSLog(@"%@",error);
    }];
    [self sendUISignal:self.RELOADING];
}

@end

@implementation getUserInfoByIm
-(void)firstPage{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:@"http://www.dddd.gg/index.php/pet/getUserInfoByIm" parameters:@{@"user_id":self.imusername} success:^(AFHTTPRequestOperation *operation, id responseObject){
        self.code = [responseObject objectForKey:@"code"];
        self.msg = [responseObject objectForKey:@"msg"];
        self.data = [responseObject objectForKey:@"data"];
        [self sendUISignal:self.RELOADED];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSDictionary *tt = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        self.code = [tt objectForKey:@"code"];
        self.msg = [tt objectForKey:@"msg"];
        [self sendUISignal:self.FAILED];
    }];
}


@end